package com.zy.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.apache.log4j.Logger;

import sun.rmi.runtime.Log;

/**
 * Servlet Filter implementation class EncodingFile
 */
@WebFilter("/*")
public class EncodingFile implements Filter {
	//使用步骤
	//1先创建一个小log
	Logger log = Logger.getLogger(this.getClass());
    /**
     * Default constructor. 
     */
    public EncodingFile() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
	//记录服务器关闭
		log.info("服务器关闭了");
		
	
	
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		/*   /*拦截所有请求，先执行该过滤器
		 * 
		 */
		//设置request 和response的编码 解决请求和响应乱码问题
		request.setCharacterEncoding("utf-8");
		
		response.setContentType("text/html;charset=utf-8");
		
		
		chain.doFilter(request, response);//一定放行
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		//记录服务器开启
				log.info("服务器开启了");
	}

}
